//
//  SourceDetailController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 16/4/27.
//  Copyright © 2016年 sgone. All rights reserved.
//

#import "SourceDetailController.h"
#import "FDInquiryView.h"
#import "FDConfirmButton.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "Toast+UIView.h"
#import "LoginViewController.h"
#import "HPGoodsDetailsScrollView.h"

@interface SourceDetailController ()<UIScrollViewDelegate,confirmClickDelegate,inquiryViewConfirmClickDelegate>
{


}



@property (nonatomic, strong) FDConfirmButton *inquiryButton;

@property (strong, nonatomic) HPGoodsDetailsScrollView *scrollView;

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@property(nonatomic,strong) FDInquiryView *inquiryView;




@end

@implementation SourceDetailController



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.backBtn.hidden = NO;
    self.titleLabel.text = @"商品详情";
    self.scrollView = [[HPGoodsDetailsScrollView alloc] initWithFrame:CGRectMake(0, 64,ViewWidth, ViewHeight - 64)];
    
    self.scrollView.messageDic = self.messageDic;
    
    CGFloat height = [self.scrollView setViewsFrameAndReturnHeight];
    

    

    
    
    self.scrollView.contentSize = CGSizeMake(ViewWidth, height + 45 / FDLaOt);
    
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    self.inquiryButton = [[FDConfirmButton alloc] initWithFrame:CGRectMake(0, ViewHeight - 40 / FDLaOt, ViewWidth, 40 / FDLaOt)];
    self.inquiryButton.delegate = self;
    [self.inquiryButton setTitle:@"点击询价" forState:UIControlStateNormal];
    [self.inquiryButton setImage:[UIImage imageNamed:@"编写"] forState:UIControlStateNormal];
    self.inquiryButton.backgroundColor = [UIColor blackColor];
    self.inquiryButton.alpha = 0.6;
    [self.view addSubview:self.inquiryButton];
    
}


- (void)confirmClick {
    
    self.inquiryView = [[FDInquiryView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
     self.inquiryView.delegate = self;
    [ self.inquiryView setCustomAlertViewWithTitle:self.messageDic[@"shop_name"] message:self.messageDic[@"username"] unit:self.messageDic[@"least_unit"] cancel:@"取消" verify:@"确定"];
    [self.view addSubview: self.inquiryView];
}

- (void)inquiryViewConfirmClick:(NSDictionary *)dic {
    
    [self setInquiryMessageByPid:self.messageDic[@"id"] nums:dic[@"num"] chart_text:dic[@"message"]];
}

- (void)setInquiryMessageByPid:(NSString *)pid nums:(NSString *)nums chart_text:(NSString *)chart_text {
    
    
    if (nums.length == 0) {
        
        [self.inquiryView makeToast:@"需求量不能为空"];
        return;
    }
    else if (chart_text.length == 0)
    {
        
        
        [self.inquiryView makeToast:@"备注不能为空"];
        return;
        
        
    }
    
    [self.inquiryView removeFromSuperview];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@add_chart_log",SERVER_ADDRESS3];
    NSDictionary *parms = @{@"uid" :  [self.dataHandler.userDic objectForKey:@"wId"] ,@"user_type" : @"3" , @"pid" : pid, @"nums" : nums , @"chart_text" : chart_text,@"chart_type"  : @"2"};
    

    
    [MBProgressHUD showHUDAddedTo:self.scrollView animated:YES];

 
    [self.manager POST:urlStr parameters:parms success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"erorr_flag"] intValue] == 0) {
            
            
            //发送通知成功进行操作对数字进行刷新
            [[NSNotificationCenter defaultCenter] postNotificationName:@"NoticeNum2" object:nil];
            
            
            [MBProgressHUD hideAllHUDsForView:self.scrollView animated:YES];
            
            
            [self.view makeToast:@"发送成功"];
            
            
            
        }
        else
        {
            
            [MBProgressHUD hideAllHUDsForView:self.scrollView animated:YES];
            NSString *str = responseObject[@"result_msg"];
            [self.view makeToast:str];
            
        }

        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        [MBProgressHUD hideAllHUDsForView:self.scrollView animated:YES];
        [self.view makeToast:@"请检查网络"];

        
    }];
    
    
    
    
    
    
    
}

@end
